package org.kineticsproject.visualizations;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import javax.swing.Timer;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.util.Rotation;

import data.Exercise;

public final class HeartRatePieChart
{
	public JFreeChart setupChart(Exercise ex, String chartTitle)
	{

		ChartPanelExt           chartPanel		= null;
		JPanelExt               contentPanel   	= null;
		contentPanel                           	= new JPanelExt(new GridBagLayout());
		
	 	NumberFormat nf;
	 	nf = new DecimalFormat();
	 	nf.setMaximumFractionDigits(2);
        DefaultPieDataset result = new DefaultPieDataset();
        
        result.setValue("VO2 Max "+nf.format(ex.getVo2maxPercentage())+"%", new Double(ex.getVo2maxPercentage()));
        result.setValue("Anaerobic "+nf.format(ex.getAnaerobicPercentage())+"%", new Double(ex.getAnaerobicPercentage()));
        result.setValue("Aerobic "+nf.format(ex.getAerobicPercentage())+"%", new Double(ex.getAerobicPercentage()));
        result.setValue("Fitness "+nf.format(ex.getRecoveryPercentage())+"%", new Double(ex.getRecoveryPercentage())); 
        result.setValue("Healthy Heart "+nf.format(ex.getOtherPercentage())+"%", new Double(ex.getOtherPercentage()));
        
  
   	        
        JFreeChart chart = ChartFactory.createPieChart3D(
                "Heart Rate Distribution",  // chart title
                result,                // dataset
                true,                   // include legend
                true,
                true
            );

        PiePlot3D plot = (PiePlot3D) chart.getPlot();
        java.awt.Font labelFont = new java.awt.Font("label",java.awt.Font.BOLD,12);
        plot.setLabelFont(labelFont);
        plot.setStartAngle(270);
        plot.setDirection(Rotation.ANTICLOCKWISE);
        plot.setForegroundAlpha(0.60f);
       // plot.setInteriorGap(0.33);
       /* GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 1.0;
        c.weighty = 1.0;
        c.fill = GridBagConstraints.BOTH;
        c.anchor = GridBagConstraints.FIRST_LINE_START;
        c.insets = new Insets(12, 12, 0, 12);
        
       //pieChart = new JFreeChart(plot);
        chartPanel = new ChartPanelExt(chart,chartTitle,tabTitle);
        contentPanel.setTabTitle(tabTitle);
        
       
        contentPanel.add(chartPanel, c);
         */   

      //Rotator rotator = new Rotator((PiePlot3D) chart.getPlot());
      // rotator.start();

		return(chart);
	}
	
	/**
	 * The rotator.
	 */
	class Rotator extends Timer implements ActionListener 
	{

	    /** The plot. */
	    private PiePlot3D plot;

	    /** The angle. */
	    private int angle = 270;

	    /**
	     * Constructor.
	     *
	     * @param plot  the plot.
	     */
	    Rotator(PiePlot3D plot) 
	    {
	        super(100, null);
	        this.plot = plot;
	        addActionListener(this);
	    }

    public void actionPerformed(ActionEvent event) 
    {
        this.plot.setStartAngle(this.angle);
        this.angle = this.angle + 1;
        if (this.angle == 360) 
        {
            this.angle = 0;
        }
    }
	}
}

